package org.jsola.hr.web;

import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.jsola.admin.entity.SiteDO;
import org.jsola.admin.service.IAsyncTaskService;
import org.jsola.admin.vo.HeaderFieldsVO;
import org.jsola.common.FileKit;
import org.jsola.contract.dto.ContractUpdateDTO;
import org.jsola.contract.dto.EnterpriseSignDTO;
import org.jsola.contract.dto.IssuedCategoryAddDTO;
import org.jsola.contract.dto.PublishSignDTO;
import org.jsola.contract.entity.ContractDO;
import org.jsola.contract.query.ContractGroupQuery;
import org.jsola.contract.query.ContractQuery;
import org.jsola.contract.query.SealQuery;
import org.jsola.contract.service.IAppendixService;
import org.jsola.contract.service.IContractService;
import org.jsola.contract.vo.*;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.file.common.FileUtil;
import org.jsola.file.entity.FileVersionDO;
import org.jsola.hr.common.CacheKeyKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.HrCategoryUpdateDTO;
import org.jsola.hr.dto.HrContractDownloadDTO;
import org.jsola.hr.dto.TemplateVariableUpdateDTO;
import org.jsola.hr.dto.contract.AddSealDTO;
import org.jsola.hr.dto.contract.UpdateSealDTO;
import org.jsola.hr.dto.contract.VerifyEnterpriseDTO;
import org.jsola.hr.entity.CompanyDO;
import org.jsola.hr.entity.EmpContractDO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.provider.IAdminProviderService;
import org.jsola.hr.provider.IContractProviderService;
import org.jsola.hr.query.HrContractExportQuery;
import org.jsola.hr.service.*;
import org.jsola.hr.service.impl.AsyncCompanyFature;
import org.jsola.hr.vo.FailCategoryVO;
import org.jsola.hr.vo.HrContractListVO;
import org.jsola.hr.vo.HrVariableListVO;
import org.jsola.permission.exception.PermissionException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.jsola.file.constant.FileConstants.DOWNLOAD_TYPE_ATTACHMENT;
import static org.jsola.hr.constant.HrPermissionConstants.HR_ENTERPRISEOVERVIEW_CONTRACT;
import static org.jsola.hr.constant.HrPermissionConstants.PERMISSION_DENIED_MESSAGE;

/**
 * @author JanusMix
 */
@RestController("hrContractController")
@RequestMapping(HrConstants.API_PATH + "/v1/contracts")
@Slf4j
public class ContractController implements IContractControllerAPI {

    @Autowired
    private IContractProviderService contractProviderService;

    @Autowired
    private IHrPermissionService hrPermissionService;
    @Autowired
    private IEmpContractService empContractService;
    @Autowired
    private IHrContractService hrContractService;

    @Autowired
    private IAsyncTaskService asyncTaskService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private AsyncCompanyFature asyncCompanyFature;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private IContractService contractService;

    @Autowired
    private IAdminProviderService adminProviderService;

    @Autowired
    private IAppendixService appendixService;

    @Autowired
    private IEmpInfoService empInfoService;


    @GetMapping(value = "/listAll")
    public Result<List<SealListVO>> listAll(@Valid SealQuery sealQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看印章权限校验

        return Result.success(
                contractProviderService.selectSeal(sealQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping("/seal")
    public Result<SealVO> saveSeal(@Valid @RequestBody AddSealDTO addSealDTO) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(contractProviderService.saveSeal(addSealDTO, tokenUser));
    }

    @Override
    @PutMapping("/seal")
    public Result<Integer> updateSeal(@Valid @RequestBody UpdateSealDTO sealUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(contractProviderService.updateSeal(sealUpdateDTO, tokenUser));
    }

    @Override
    @GetMapping("/seal/{sealId}")
    public Result<SealVO> selectSealById(@PathVariable Long sealId) {
        TokenUser tokenUser = UserContext.getUser();
        ParamException.notNull(sealId, "印章id不能为空");
        return Result.success(contractProviderService.selectSealById(sealId, tokenUser.getSiteId()));
    }

    @Override
    @GetMapping("/seal")
    public Result<List<SealListVO>> selectSealList(@Valid SealQuery sealQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(contractProviderService.sealSealList(sealQuery, tokenUser.getSiteId()));
    }

    /**
     * 企业验证接口
     * 该方法用于验证企业信息的合法性
     * 它接收一个 VerifyEnterpriseDTO 对象作为请求体参数，该对象包含了需要验证的企业信息
     * 使用 @Valid 注解对传入的企业信息进行合法性校验，确保数据的完整性和有效性
     * @PostMapping("/verifyEnterprise") 注解表明这是一个处理 POST 请求的接口，路径为 /verifyEnterprise
     *
     * @param verifyEnterpriseDTO 企业验证数据传输对象，包含了需要验证的企业信息
     * @return 返回一个 Result<String> 对象，表示验证结果
     *         如果验证成功，返回成功结果，否则返回相应的错误信息
     */
    @Override
    @PostMapping("/verifyEnterprise")
    public Result<String> verifyEnterprise(@Valid @RequestBody VerifyEnterpriseDTO verifyEnterpriseDTO) {
        // 获取当前操作用户的信息，用于记录操作者
        TokenUser tokenUser = UserContext.getUser();
        // 调用 contractProviderService 的 verifyEnterprise 方法进行企业验证
        contractProviderService.verifyEnterprise(verifyEnterpriseDTO, tokenUser);
        // 验证成功，返回成功结果
        return Result.success();
    }

    @Override
    @GetMapping("/contact/myContract")
    public Result<List<PartySignListVO>> selectUserContractList() {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(contractProviderService.selectUserContractList(tokenUser));
    }

    @Override
    @GetMapping("/categories/{categoryId}")
    public Result<CategoryVO> selectCategoryById(@PathVariable Long categoryId) {
        TokenUser tokenUser = UserContext.getUser();
        ParamException.notNull(categoryId, "类型id不能为空");
        return Result.success(contractProviderService.selectCategoryById(categoryId, tokenUser.getSiteId()));
    }

    @Override
    @GetMapping("/template/{templateId}/variableList")
    public Result<List<Map<String, Object>>> selectVariableList(@PathVariable Long templateId) {
        TokenUser tokenUser = UserContext.getUser();
        ParamException.notNull(templateId, "类型id不能为空");
        return Result.success(contractProviderService.getVariableMapList(templateId, tokenUser.getSiteId()));
    }

    @Override
    @PostMapping("/downloadContract")
    public ResponseEntity<Resource> downloadContract(@Valid @RequestBody HrContractDownloadDTO hrContractDownloadDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // 查询合同文件,并进行打包
        FileVersionDO fileVersionDO = contractProviderService.downloadContract(hrContractDownloadDTO, tokenUser);
        FileSystemResource resource = new FileSystemResource(fileVersionDO.getTempFilePath());
        if (!resource.exists()) {
            throw new ParamException("系统错误，文件不存在或已被删除");
        }
        // 文件名
        String name = fileVersionDO.getName();
        // 文件名编码
//        String filenames = new String(name.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        String filenames = FileKit.getNameAndEncode(name);
        MediaType mimeType = FileUtil.guessMimeType(name);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, DOWNLOAD_TYPE_ATTACHMENT + ";filename=\"" + filenames + "\"")
                .contentType(mimeType)
                .body(resource);
    }

    @Override
    @DeleteMapping("/deleteContractAndRelationById/{contractId}")
    public Result<String> deleteContractAndRelationById(@PathVariable Long contractId) {
        TokenUser tokenUser = UserContext.getUser();

        // 用户是不是admin
        boolean userIsAdmin = hrPermissionService.isAdmin(tokenUser.getUserId(), tokenUser.getSiteId());
        if (!userIsAdmin) {
            return Result.fail("权限不足,请联系超级管理员删除合同");
        }

        contractProviderService.deleteContractAndRelationById(contractId, tokenUser);
        return Result.success("删除成功");
    }

    @Override
    @PostMapping("/exportContractBySubjectId")
    public ResponseEntity<Resource> exportContractBySubjectId(@RequestBody HrContractExportQuery hrContractExportQuery) {
        ParamException.hasText(hrContractExportQuery.getSubjectId(), "公司id不能为空");
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验
        hrContractExportQuery.setEnName("contract");
        String excelFilePath = contractProviderService
                .selectExportContractList(hrContractExportQuery, tokenUser);
        // 导出excel
        // 编码文件名，防止乱码
        String filenames = new String(FilenameUtils.getName(excelFilePath).getBytes(StandardCharsets.UTF_8),
                StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }

    @Override
    @PostMapping("/exportAllContract")
    public ResponseEntity<Resource> exportAllContract(@RequestBody HrContractExportQuery hrContractExportQuery) {
        TokenUser tokenUser = UserContext.getUser();
//        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId(),
//                HR_ENTERPRISEOVERVIEW_CONTRACT), PERMISSION_DENIED_MESSAGE);
        hrContractExportQuery.setEnName("contractAll");
        String excelFilePath = contractProviderService
                .selectExportContractList(hrContractExportQuery, tokenUser);
        // 导出excel
        // 编码文件名，防止乱码
        String filenames = new String(FilenameUtils.getName(excelFilePath).getBytes(StandardCharsets.UTF_8),
                StandardCharsets.ISO_8859_1);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }

    @Override
    @GetMapping("/getComCount")
    public Result<Page<ContractStatisticsEmpUnitsVO>> selectComCountPage(ContractQuery contractQuery) {
        TokenUser tokenUser = UserContext.getUser();
        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId(),
                HR_ENTERPRISEOVERVIEW_CONTRACT), PERMISSION_DENIED_MESSAGE);

        return Result.success(
                contractProviderService.selectComForRankCount(contractQuery, tokenUser)
        );
    }

    @Override
    @PostMapping("/getComCountPost")
    public Result<Page<ContractStatisticsEmpUnitsVO>> selectComForRankCountPost(@RequestBody ContractQuery contractQuery) {
        TokenUser tokenUser = UserContext.getUser();
        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId(),
                HR_ENTERPRISEOVERVIEW_CONTRACT), PERMISSION_DENIED_MESSAGE);

        return Result.success(
                contractProviderService.selectComForRankCountPost(contractQuery, tokenUser)
        );
    }


    @Override
    @GetMapping(value = "/pageContract")
    public Result<Page<ContractListVO>> pageContract(ContractQuery contractQuery) {
        TokenUser tokenUser = UserContext.getUser();
        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId(),
                HR_ENTERPRISEOVERVIEW_CONTRACT), PERMISSION_DENIED_MESSAGE);

        return Result.success(
                contractProviderService.selectPageContract(contractQuery, tokenUser)
        );
    }

    @Override
    @PostMapping("/selectPageBySubjectIds")
    public Result<Page<HrContractListVO>> selectPageBySubjectIds(@RequestBody ContractQuery contractQuery) {
        TokenUser tokenUser = UserContext.getUser();
        PermissionException.isTrue(hrPermissionService.hasRight(tokenUser.getUserId(), tokenUser.getSiteId(),
                HR_ENTERPRISEOVERVIEW_CONTRACT), PERMISSION_DENIED_MESSAGE);

        return Result.success(
                contractProviderService.selectPageBySubjectIds(contractQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectVariableNotPersonal")
    public Result<List<HrVariableListVO>> pageContract(String companyId, String fieldName) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                contractProviderService.selectVariableNotPersonal(companyId, fieldName, tokenUser)
        );

    }


    @Override
    @PostMapping(value = "/updateTemplateVariable")
    public Result<String> updateTemplateVariable(@Valid @RequestBody TemplateVariableUpdateDTO templateVariableUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();

        log.info("合同关联变量修改,合同模板Id：{},其余信息：{}", templateVariableUpdateDTO.getTemplateId(), templateVariableUpdateDTO);
        contractProviderService.updateTemplateVariable(templateVariableUpdateDTO, tokenUser);
        return Result.success("保存成功");
    }

    @Override
    @PutMapping("/setJpgToAppendix/{contractId}")
    public Result<Integer> setJpgToAppendix(@PathVariable Long contractId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(contractProviderService.setJpgToAppendix(contractId, tokenUser));
    }

    @Override
    @PutMapping("/setJpgToMoreAppendix")
    public Result<Integer> setJpgToMoreAppendix(@RequestBody List<Long> contractIds) {
        TokenUser tokenUser = UserContext.getUser();
        ParamException.notNull(contractIds, "合同id List 不能为空");
        for (Long contractId : contractIds) {
            contractProviderService.setJpgToAppendix(contractId, tokenUser);
        }
        return Result.success(contractIds.size());
    }

    @Override
    @PostMapping("saveIssuedCategory")
    public Result<List<FailCategoryVO>> saveIssuedCategory(@Valid @RequestBody IssuedCategoryAddDTO issuedCategoryAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增合同类型权限校验

        return Result.success(
                contractProviderService.saveIssuedCategory(issuedCategoryAddDTO, tokenUser)
        );
    }

    /**
     * 合同模板关联默认字段转化为必填字段
     *
     * @param siteId 指定租户 没有指定同步所有租户
     * @return 无
     */
    @PostMapping("issuedDefaultVariable")
    public Result<Object> issuedDefaultVariable(String siteId) {

        Map<String, SiteDO> siteMap = adminProviderService.selectSiteMap();
        Set<String> siteIds;
        if (siteId == null) {
            siteIds = siteMap.keySet();
        } else {
            siteIds = Collections.singleton(siteId);
        }
        for (String userSiteId : siteIds) {
            TokenUser tokenUser = new TokenUser("issuedDefaultVariable", userSiteId);
            contractProviderService.issuedDefaultVariable(tokenUser);
        }

        return Result.success();
    }

    @Override
    @GetMapping(value = "/getSignedRecordHead")
    public Result<HeaderFieldsVO> getSignedRecordHead(String companyId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                contractProviderService.getSignedRecordHead(companyId, tokenUser)
        );

    }

    @Override
    @GetMapping(value = "/getAdminSignedRecordHead")
    public Result<HeaderFieldsVO> getAdminSignedRecordHead(String companyId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                contractProviderService.getAdminSignedRecordHead(companyId, tokenUser)
        );

    }

    @Override
    @PutMapping("/updateContractName")
    public Result<Integer> updateContractName(@RequestBody @Valid ContractUpdateDTO contractUpdateDTO) {
        // 参数只有Id和name！！！cao！！！！
        Long contractId = contractUpdateDTO.getId();
        String contractName = contractUpdateDTO.getName();
        TokenUser tokenUser = UserContext.getUser();

        ParamException.notNull(contractId, "修改合同Id不能为空");
        ParamException.isFalse(StringUtil.isBlank(contractName), "修改后合同名称不能为空");

        // 修改合同表 只能修改名称字段
        ContractUpdateDTO updateDTO = new ContractUpdateDTO();
        updateDTO.setId(contractId);
        updateDTO.setName(contractName);
        int count = contractProviderService.updateContract(updateDTO, tokenUser);

        // 同步修改花名册合同表
        List<EmpContractDO> empContractDOList = empContractService.selectByContractId(contractId);
        for (EmpContractDO empContractDO : empContractDOList) {
            empContractDO.setName(contractName);
            empContractService.updateByIdSelective(empContractDO);
        }
        contractProviderService.updateFileName(contractId, contractName, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);

    }


    @Override
    @PutMapping("/updateContractNo")
    public Result<Integer> updateContractNo(@RequestBody @Valid ContractUpdateDTO contractUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();

        ParamException.notNull(contractUpdateDTO.getId(), "修改合同Id不能为空");
        if (StringUtil.isBlank(contractUpdateDTO.getContractNo())) {
            contractUpdateDTO.setContractNo("");
        }
        // 修改合同表 只能修改名称字段
        ContractUpdateDTO updateDTO = new ContractUpdateDTO();
        updateDTO.setId(contractUpdateDTO.getId());
        updateDTO.setContractNo(contractUpdateDTO.getContractNo());
        int count = contractProviderService.updateContract(updateDTO, tokenUser);

        // 同步修改花名册合同表
        List<EmpContractDO> empContractDOList = empContractService.selectByContractId(contractUpdateDTO.getId());
        for (EmpContractDO empContractDO : empContractDOList) {
            empContractDO.setRemark(contractUpdateDTO.getContractNo());
            empContractService.updateByIdSelective(empContractDO);
        }

        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);

    }

    @Override
    @PutMapping("/updateContractNameOrContractNo")
    public Result<Integer> updateContractNameOrContractNo(@RequestBody @Valid ContractUpdateDTO contractUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();

        ParamException.notNull(contractUpdateDTO.getId(), "修改合同Id不能为空");
        // 修改合同表 修改名称字段、备注
        ContractUpdateDTO updateDTO = new ContractUpdateDTO();
        updateDTO.setId(contractUpdateDTO.getId());
        updateDTO.setName(contractUpdateDTO.getName());
        updateDTO.setContractNo(contractUpdateDTO.getContractNo());
        int count = contractProviderService.updateContract(updateDTO, tokenUser);

        // 同步修改花名册合同表
        List<EmpContractDO> empContractDOList = empContractService.selectByContractId(contractUpdateDTO.getId());
        for (EmpContractDO empContractDO : empContractDOList) {
            empContractDO.setName(contractUpdateDTO.getName());
            empContractDO.setRemark(contractUpdateDTO.getContractNo());
            empContractService.updateByIdSelective(empContractDO);
        }

        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @PostMapping("/publishTemplateSign")
    public Result<List<ContractVO>> publishTemplateSign(@Valid @RequestBody PublishSignDTO publishSignDTO) {
        TokenUser tokenUser = UserContext.getUser();

        List<ContractVO> contractVOList = contractProviderService.publishTemplateSign(publishSignDTO, tokenUser);
        return Result.success(contractVOList);
    }

    @Override
    @PostMapping("/publishTemplateSignAsync")
    public Result<String> publishTemplateSignAsync(@Valid @RequestBody PublishSignDTO publishSignDTO) {
        TokenUser tokenUser = UserContext.getUser();
        if (CollectionUtils.isEmpty(publishSignDTO.getMemberVariableList())) {
            throw new ParamException("发起合同，人员参数不能为空");
        }
        return Result.success(contractProviderService.publishTemplateSignAsync(publishSignDTO, tokenUser));
    }

    @Override
    @PostMapping("/enterpriseSignAgainBatch")
    public Result enterpriseSignAgainBatch(@Valid @RequestBody EnterpriseSignDTO enterpriseSignDTO) {
        TokenUser tokenUser = UserContext.getUser();
        List<Long> contractIds = enterpriseSignDTO.getContractIds();
        if (CollectionUtils.isEmpty(contractIds)) {
            throw new ParamException("请选择需要公司确认签署的合同");
        }

        ContractDO contractDO = contractService.selectDOById(contractIds.get(0), tokenUser.getSiteId());
        String companyId = contractDO.getSubjectId();
        CompanyDO companyDO = companyService.selectDOById(companyId, tokenUser.getSiteId());
        if (companyDO == null) {
            throw new ParamException("公司id不存在");
        }

        String key = CacheKeyKit.getEnterpriseSignKey(companyId);

        RLock lock = redissonClient.getLock(key);
        if (lock.isLocked()) {
            throw new HrException("正在确认签署合同，请稍后操作");
        }
        //companyId 只指用工单位id, enterpriseSignDTO.getCompanyId() 是合同模板印章中认证的公司信息
        asyncCompanyFature.enterpriseSignAgainBatch(contractIds, companyId, enterpriseSignDTO.getCompanyId(), tokenUser);
        return Result.success();
    }

    @PostMapping("/demoAsync")
    public Result<String> demoAsync(@Valid @RequestBody PublishSignDTO publishSignDTO) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(contractProviderService.demoAsync(publishSignDTO, tokenUser));
    }

    @Override
    @DeleteMapping("/deleteContractBatch")
    public Result<Integer> deleteContractBatch(@RequestBody Long[] contractIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除合同权限校验

        int count = contractProviderService.deleteContractByIds(tokenUser, contractIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @PutMapping(value = "/selectAndSynchronizeContractGroup")
    public Result<List<ContractGroupListVO>> selectAndSynchronizeContractGroup(@Valid @RequestBody ContractGroupQuery contractGroupQuery) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                contractProviderService.selectAndSynchronizeContractGroup(contractGroupQuery, tokenUser)
        );

    }

    @Override
    @PutMapping(value = "/updateCategoryByCompany")
    public Result<Integer> updateCategoryByCompany(@Valid @RequestBody HrCategoryUpdateDTO hrCategoryUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();

        hrContractService.updateCategoryByCompany(hrCategoryUpdateDTO, tokenUser);

        return Result.success(
        );

    }

    /**
     * 修复合同变量
     */
    @PostMapping(value = "/repairTemplateVariable")
    public Result<Integer> repairTemplateVariable() {

        contractProviderService.repairTemplateVariable();

        return Result.success(
        );

    }

    /**
     * 获取合同签署链接
     */
    @GetMapping("/getUrl")
    public Result<String> getUrl(@RequestParam Long contractId, @RequestParam Integer type) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                contractProviderService.getUrl(contractId, type, tokenUser)
        );
    }
}
